<template>
  <div>
    <el-drawer
      title="添加学生"
      :visible.sync="dialogFormVisible"
      size="800px"
      @close="close"
    >
      <el-form ref="form" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="班级" prop="classId">
          <el-select v-model="formData.classId" placeholder="请选择班级">
            <el-option
              v-for="item in classList"
              :key="item.id"
              :label="item.className"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择日期" prop="courseDate">
          <el-date-picker
            v-model="formData.courseDate"
            type="datetimerange"
            align="right"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['09:00:00', '22:00:00']"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="学生" prop="studentIds">
          <el-checkbox-group v-model="formData.studentIds">
            <el-checkbox
              v-for="item in studentList"
              :key="item.id"
              :label="item.id"
            >
              {{ item.name }}
            </el-checkbox>
          </el-checkbox-group>
          <el-button @click="handleOpenEditor('studentEditor')">
            新增学生
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="close">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>

    <StudentEditor ref="studentEditor" @on-success="getStudentList" />
  </div>
</template>

<script>
  import { createCourse, updateCourse } from '@/apis/course'
  import { getClassByPage } from '@/apis/class'
  import { getStudentByPage } from '@/apis/student'
  import StudentEditor from '@/components/Editor/StudentEditor'

  export default {
    components: {
      StudentEditor,
    },
    data() {
      return {
        formData: {
          id: null,
          classId: null,
          courseDate: [],
          studentIds: [],
        },
        classList: [],
        studentList: [],
        rules: {
          classId: [
            { required: true, trigger: 'change', message: '请选择班级' },
          ],
          startDate: [
            { required: true, trigger: 'change', message: '请选择开始日期' },
          ],
          studentIds: [
            { required: true, trigger: 'change', message: '请选择学生' },
          ],
        },
        dialogFormVisible: false,
      }
    },
    methods: {
      init() {
        this.getClassList()
        this.getStudentList()
      },
      getClassList() {
        getClassByPage().then(({ list }) => {
          this.classList = list
        })
      },
      getStudentList() {
        getStudentByPage().then(({ list }) => {
          this.studentList = list
        })
      },
      showEdit() {
        this.init()
        this.dialogFormVisible = true
      },
      handleOpenEditor(name) {
        this.$refs[name].showEdit()
      },
      handleOpenTeacherFreeCalendar() {
        this.$refs.teacherFreeCalendarRef.open()
      },
      close() {
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { classId, courseDate, studentIds } = this.formData
            const data = {
              classId,
              startDate: courseDate[0],
              endDate: courseDate[1],
              studentIds,
            }

            await updateCourse(data)
            this.$baseMessage('操作成功', 'success')
            this.$emit('on-success')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
